package com.genius.unite_platform.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.FillAddJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.unite_platform.device.entity.DeviceEntity;
import com.genius.unite_platform.device.service.DeviceServiceAdapter;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;

import org.apache.poi.util.SystemOutLogger;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Date;

@Service("DeviceInsertService")
public class DeviceInsertServiceImpl extends DeviceServiceAdapter{

	@Override
	public JSONObject add(JSONObject json, JSONObject response,HttpSession session) {    	
    	// 先查询主键是否存在
    	try {  
    		String keywords = DeviceEntity.deviceno;
	    	String insertTable = "device";
			JSONObject existJson = DBTools2.getIsExist(keywords,insertTable, json);			
			if(existJson != null){
				response.put(ResponseJson.msg, "已存在该数据");
				response.put(ResponseJson.status, ResponseJson.status_Failed);
				return response;
			}	
			
			JSONObject userJson = (JSONObject)session.getAttribute(SessionJson.currentUser);			
			json.put(DeviceEntity.lrr, userJson.getString(UserJson.name));			
			String insertField = DeviceEntity.deviceno+","+DeviceEntity.devicename+","+DeviceEntity.memo+","+DeviceEntity.lrr+","+DeviceEntity.lrsj+"=now()";		
						
			DBTools2.insert(insertField, insertTable,json);			
			String itemField = "deviceno,modelId,modelname,modeltypeId,modeltype,imgs,lrr,lrsj";
			JSONArray jsonArray = new JSONArray();
			for(int i=0;i<Integer.parseInt(json.getString("totalNum"));i++){				
				JSONObject jsonObject = new JSONObject();
				String   typeid=json.getString("type_"+i);
				String   typename="";
				String   flag =json.getString("flag_"+i);
		        if (flag.equals("add"))
		        {
		        	String modelId=json.getString("id_"+i);
		        	if (!modelId.equals(""))
		        	{		        	  
			        	if (typeid.equals("1"))
			         	{
					         typename="公司";
				         }
			         	if (typeid.equals("2"))
			        	{
				        	typename="厂家";
				         }
				         if (typeid.equals("3"))
				         {
					          typename="自有";
				          }	
				
			    	   Date day=new Date();
			           SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
			    	   jsonObject.put("deviceno",json.getString("deviceno"));
			           jsonObject.put("modelId",modelId);
			           jsonObject.put("modelname",json.getString("name_"+i));		
			    	   jsonObject.put("modeltypeId",typeid);	
			           jsonObject.put("modeltype",typename);
			    	   jsonObject.put("imgs",json.getString("img_"+i));	
			           jsonObject.put("lrr",userJson.getString(UserJson.name));	
			    	   jsonObject.put("lrsj",sdf.format(day));	
			    	   jsonArray.add(jsonObject);
			    	   System.out.println(jsonObject);
		        	}
		        }
			}				

			DBTools2.insertBatch(itemField,"devicemodel",jsonArray);
			
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Error);
		}
		return response;		
    
	}

	@Override
	public JSONObject fillAdd(JSONObject json, JSONObject response) {   
	try {
    		JSONObject addJson = new JSONObject();
			
			String selectField = "";
    		String whereField = "";
    		String table = "";
			HashMap<String, String> sqlMap = new HashMap<>();			
			
			JSONObject fillJson = new JSONObject();
			
			addJson = DBTools.getFilling(selectField,whereField,table,json,addJson);			
			JSONObject fillEditJson = DBTools.getFilling(sqlMap);					
			fillJson.put(FillAddJson.field, addJson);
			fillJson.put(FillAddJson.select, fillEditJson);
			
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Success);
			response.put(ResponseJson.data,fillJson);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Fill_Data_Error);
		}
		return response;
	}

}

